package com.ny.service.impl;

import com.ny.entity.User;
import com.ny.mapper.UserMapper;
import com.ny.service.EmailService;
import com.ny.service.UserService;
import com.ny.until.PasswordUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmailService emailService;
       @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<User> findAllUser() {

        return userMapper.findAllUser();
    }

    @Override
    public User login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public User findUserByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

    @Override
    public int registerUser(User user) {
        // 密码加密，需要一个密码加密的工具类
        return userMapper.registerUser(user);
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }


    @Override
    public void sendEmailCode(String email) {
        //生成验证码
        String code = emailService.generateCode();
        // 把验证码存储到redis数据中,进行验证和过期的
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(email, code,10, TimeUnit.MINUTES);
        emailService.sendEmail(email, code);
    }
    @Override
    public int updatePasswordByEmail(String email, String password) {
        // 密码加密
        password = PasswordUntil.encryptPassword(password);
        //        redisTemplate.delete(email);清楚redis的验证码
        return userMapper.updatePasswordByEmail(email, password);
    }
    @Override
    public boolean checkCode(String email, String code) {
        // 从redis中获取验证码
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String redisCode = ops.get(email);
        if (redisCode != null && redisCode.equals(code)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePasswordByUsername(String username, String oldPassword, String newPassword) {
        // 先通过用户名查找用户
        User user = userMapper.findUserByUsername(username);
        if (user == null) {
            return false;
        }
        
        // 验证旧密码是否正确
        String encryptedOldPassword = PasswordUntil.encryptPassword(oldPassword);
        if (!encryptedOldPassword.equals(user.getPassword())) {
            return false;
        }
        
        // 加密新密码并更新
        String encryptedNewPassword = PasswordUntil.encryptPassword(newPassword);
        return userMapper.updatePasswordByUsername(username, encryptedNewPassword) > 0;
    }
    
    @Override
    public boolean updateNicknameByUsername(String username, String nickname) {
        return userMapper.updateNicknameByUsername(username, nickname) > 0;
    }
}